﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using N2.Web;
using FlexiCommerce.Components;
using N2.Collections;
using FlexiCommerce.Web.Templates;
using FlexiCommerce.Web.Addons.FC.Items;
namespace FlexiCommerce.Web.Addons.FC.Components
{
    public class UrlParser : IUrlParser
    {
        private IUrlParser internalParser;

        public UrlParser()
        {
            this.internalParser = new Web.UrlParser(Context.Current.RequestContext);
        }

        public string BuildUrl(Category item)
        {
            TypeFilter storeTypeFilter = new TypeFilter(typeof(Items.StorePage));
            TypeFilter categoryTypeFilter = new TypeFilter(typeof(Items.CategoryPage));

            ItemList items = Templates.Find.ClosestLanguageRoot.GetChildren(storeTypeFilter);

            if (items.Count == 0)
                return "";
            items = items[0].GetChildren(categoryTypeFilter);

            if (items.Count == 0)
                return "";

            return N2.Web.Url.Parse(N2.Context.Current.UrlParser.BuildUrl(items[0])).AppendSegment(FlexiCommerce.Web.Url.Format(item.ID.ToString() + "/" + item.Name));

        }

        public Category ParseCategory(string url)
        {
            return internalParser.ParseCategory(url);
        }

        public Category CurrentCategory
        {
            get {
                PathData path = N2.Context.Current.RequestContext.CurrentPath;
                if (path != null && path.Action == "category")
                {
                    int id = 0;
                    if (path.Argument != null && path.Argument.Contains("/") && path.Argument.Length > 3)
                    {
                        string value = path.Argument.Substring(0, path.Argument.IndexOf("/"));
                        if (int.TryParse(value, out id))
                        {
                            return Find.Categories.SingleOrDefault(c => c.ID == id);
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
                return null;
            }
        }

        public System.Text.RegularExpressions.Regex CategoryRegex
        {
            get { return internalParser.CategoryRegex; }
        }

        public int ParseCategoryIdentifier(string input)
        {
            return internalParser.ParseCategoryIdentifier(input);
        }


        public string BuildUrl(Category item, System.Globalization.CultureInfo culture)
        {
            TypeFilter typeFilter = new TypeFilter(typeof(Items.StorePage));
            ItemList items = N2.Find.Items.Where.Detail("LanguageCode").Eq(culture.Name).Select()[0].GetChildren(typeFilter);
            if (items.Count == 0)
                throw new InvalidOperationException();
            CategoryPage categoryPage = (CategoryPage)items[0].GetChildren(new TypeFilter(typeof(CategoryPage)))[0];
            return new Url(N2.Context.Current.UrlParser.BuildUrl(categoryPage)).AppendSegment(item.ID + "/" + item.Name);
        }



        #region IUrlParser Members


        public string BuildUrl(Product item, System.Globalization.CultureInfo culture)
        {
            Url url = new Url(internalParser.BuildUrl(item, culture));
            TypeFilter typeFilter = new TypeFilter(typeof(Items.StorePage));
            ItemList items = N2.Find.Items.Where.Detail("LanguageCode").Eq(culture.Name).Select()[0].GetChildren(typeFilter);
            if (items.Count == 0)
                return url;
            url = new Url(N2.Context.Current.UrlParser.BuildUrl(items[0])).AppendSegment(url.PathWithoutExtension.Replace("~/", ""));
            return url;
        }

        public string BuildUrl(Product item)
        {
            Url url = new Url(internalParser.BuildUrl(item));
            TypeFilter typeFilter = new TypeFilter(typeof(Items.StorePage));
            ItemList items = Templates.Find.ClosestLanguageRoot.GetChildren(typeFilter);
            if (items.Count == 0)
                return url;
            url = new Url(N2.Context.Current.UrlParser.BuildUrl(items[0])).AppendSegment(url.PathWithoutExtension.Replace("~/", ""));
            return url;
        }

        #endregion
    }
}
